package org.loofer.ext.component.info;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import org.loofer.ext.Ext;
import org.loofer.ext.utils.NetWorkUtils;
import org.loofer.ext.utils.StringUtils;

public class WifiDash {
    public static String getBSSID() {
        WifiManager wifiManager = (WifiManager) Ext.getContext().getSystemService(NetWorkUtils.APN_NAME_WIFI);
        if (wifiManager == null) {
            return null;
        }
        WifiInfo wifiInfo;
        try {
            wifiInfo = wifiManager.getConnectionInfo();
        } catch (Exception e) {
            wifiInfo = null;
        }
        if (wifiInfo == null) {
            return null;
        }
        String bssid = wifiInfo.getBSSID();
        if (StringUtils.NOT_AVAILABLE.equals(bssid) || "00:00:00:00:00:00".equals(bssid) || "FF:FF:FF:FF:FF:FF".equalsIgnoreCase(bssid)) {
            return null;
        }
        return bssid;
    }

    public static String getSSID() {
        WifiManager wifiManager = (WifiManager) Ext.getContext().getSystemService(NetWorkUtils.APN_NAME_WIFI);
        if (wifiManager == null) {
            return null;
        }
        WifiInfo wifiInfo;
        try {
            wifiInfo = wifiManager.getConnectionInfo();
        } catch (Exception e) {
            wifiInfo = null;
        }
        if (wifiInfo != null) {
            return wifiInfo.getSSID();
        }
        return null;
    }

    public static int getNetworkId() {
        WifiManager wifiManager = (WifiManager) Ext.getContext().getSystemService(NetWorkUtils.APN_NAME_WIFI);
        if (wifiManager == null) {
            return -1;
        }
        WifiInfo wifiInfo;
        try {
            wifiInfo = wifiManager.getConnectionInfo();
        } catch (Exception e) {
            wifiInfo = null;
        }
        if (wifiInfo != null) {
            return wifiInfo.getNetworkId();
        }
        return -1;
    }

    public static int getSignalLevel() {
        String wifiInfo = queryWifiInfo(StringUtils.NOT_AVAILABLE);
        if (wifiInfo == StringUtils.NOT_AVAILABLE) {
            return -1;
        }
        return WifiManager.calculateSignalLevel(((WifiInfo) wifiInfo).getRssi(), 5);
    }

    private static Object queryWifiInfo(Object defValue) {
        WifiManager wifiManager = (WifiManager) Ext.getContext().getSystemService(NetWorkUtils.APN_NAME_WIFI);
        if (wifiManager == null) {
            return defValue;
        }
        WifiInfo wifiInfo;
        try {
            wifiInfo = wifiManager.getConnectionInfo();
        } catch (Exception e) {
            wifiInfo = null;
        }
        if (wifiInfo != null) {
            return wifiInfo;
        }
        return defValue;
    }

    public static String getWifiInfo() {
        WifiManager wifiManager = (WifiManager) Ext.getContext().getSystemService(NetWorkUtils.APN_NAME_WIFI);
        if (wifiManager == null) {
            return "[-]";
        }
        WifiInfo wifiInfo;
        try {
            wifiInfo = wifiManager.getConnectionInfo();
        } catch (Exception e) {
            wifiInfo = null;
        }
        if (wifiInfo == null) {
            return "[-]";
        }
        String ssid = wifiInfo.getSSID();
        String signal = String.valueOf(WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5));
        String speed = String.valueOf(wifiInfo.getLinkSpeed()) + " " + "Mbps";
        String bssid = wifiInfo.getBSSID();
        StringBuffer buffer = new StringBuffer();
        buffer.append('[').append(signal).append(", ").append(ssid).append(", ").append(speed).append(", ").append(bssid).append(']');
        return buffer.toString();
    }
}
